//
//  ViewController.swift
//  MVVMDemo
//
//  Created by 李煜 on 2022/1/16.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    
    let datas = [
        Person(firstName: "Tom", lastName: "clark", gender: "male", age: 18, height: 180),
        Person(firstName: "Jerry", lastName: "wer", gender: "male", age: 18, height: 180),
        Person(firstName: "Jane", lastName: "blue", gender: "female", age: 18, height: 170),
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(CustomTableViewCell.nib(), forCellReuseIdentifier: CustomTableViewCell.cellIdentifire)
        
        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        datas.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.cellIdentifire, for: indexPath) as! CustomTableViewCell
        
        let model = datas[indexPath.row]
        let viewModel = CellViewModel(firstName: model.firstName, lastName: model.lastName)
        
        cell.configure(with: viewModel)
        
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

struct Person {
    let firstName: String
    let lastName: String
    let gender: String
    let age: Int
    let height: Double
}
struct CellViewModel {
    let firstName: String
    let lastName: String
}
